package com.it.schoolhomeos.controller;

import com.it.schoolhomeos.dto.ApiResponse;
import com.it.schoolhomeos.dto.AttendanceBatchDTO;
import com.it.schoolhomeos.dto.AttendanceStatDTO;
import com.it.schoolhomeos.dto.StudentAttendanceDTO;
import com.it.schoolhomeos.service.AttendanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 考勤控制器
 * 
 * 提供考勤相关的API接口
 */
@RestController
@RequestMapping("/api")
@Tag(name = "考勤管理", description = "考勤记录的查询和录入接口")
public class AttendanceController {

    private static final Logger logger = LoggerFactory.getLogger(AttendanceController.class);

    @Autowired
    private AttendanceService attendanceService;
    
    /**
     * 获取近七天考勤统计
     */
    @GetMapping("/teacher/attendance/stats/last-seven-days")
    @Operation(summary = "获取近七天考勤统计", description = "获取当前教师班级近七天的考勤统计数据")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<List<AttendanceStatDTO>> getLastSevenDaysAttendanceStat() {
        try {
            List<AttendanceStatDTO> stats = attendanceService.getLastSevenDaysAttendanceStat();
            return ApiResponse.success("获取近七天考勤统计成功", stats);
        } catch (Exception e) {
            logger.error("获取近七天考勤统计失败", e);
            return ApiResponse.error(500, "获取近七天考勤统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取今日考勤统计
     */
    @GetMapping("/teacher/attendance/stats/today")
    @Operation(summary = "获取今日考勤统计", description = "获取当前教师班级今日的考勤统计数据")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<AttendanceStatDTO> getTodayAttendanceStat() {
        try {
            AttendanceStatDTO stat = attendanceService.getTodayAttendanceStat();
            return ApiResponse.success("获取今日考勤统计成功", stat);
        } catch (Exception e) {
            logger.error("获取今日考勤统计失败", e);
            return ApiResponse.error(500, "获取今日考勤统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取指定日期考勤统计
     */
    @GetMapping("/teacher/attendance/stats/by-date")
    @Operation(summary = "获取指定日期考勤统计", description = "获取当前教师班级指定日期的考勤统计数据")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<AttendanceStatDTO> getAttendanceStatByDate(
            @Parameter(description = "日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        try {
            AttendanceStatDTO stat = attendanceService.getAttendanceStatByDate(date);
            return ApiResponse.success("获取指定日期考勤统计成功", stat);
        } catch (Exception e) {
            logger.error("获取指定日期考勤统计失败", e);
            return ApiResponse.error(500, "获取指定日期考勤统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取指定日期学生考勤详情
     */
    @GetMapping("/teacher/attendance/details/by-date")
    @Operation(summary = "获取指定日期学生考勤详情", description = "获取当前教师班级指定日期的学生考勤详情")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<List<StudentAttendanceDTO>> getStudentAttendanceDetailsByDate(
            @Parameter(description = "日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        try {
            List<StudentAttendanceDTO> details = attendanceService.getStudentAttendanceDetailsByDate(date);
            return ApiResponse.success("获取指定日期学生考勤详情成功", details);
        } catch (Exception e) {
            logger.error("获取指定日期学生考勤详情失败", e);
            return ApiResponse.error(500, "获取指定日期学生考勤详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量提交学生考勤
     */
    @PostMapping("/teacher/attendance/batch-submit")
    @Operation(summary = "批量提交学生考勤", description = "批量提交当前教师班级学生的考勤记录")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<Integer> submitAttendanceBatch(@RequestBody AttendanceBatchDTO attendanceBatchDTO) {
        try {
            int count = attendanceService.submitAttendanceBatch(attendanceBatchDTO);
            return ApiResponse.success("批量提交学生考勤成功", count);
        } catch (Exception e) {
            logger.error("批量提交学生考勤失败", e);
            return ApiResponse.error(500, "批量提交学生考勤失败: " + e.getMessage());
        }
    }
    
    /**
     * 家长查看学生考勤记录
     */
    @GetMapping("/parent/students/{studentId}/attendance")
    @Operation(summary = "家长查看考勤", description = "家长查看自己孩子的考勤记录")
    @PreAuthorize("hasRole('PARENT')")
    public ApiResponse<?> getStudentAttendanceByParent(
            @Parameter(description = "学生ID") @PathVariable Long studentId,
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        try {
            List<StudentAttendanceDTO> attendances = attendanceService.getStudentAttendanceByParent(studentId, startDate, endDate);
            return ApiResponse.success("获取学生考勤记录成功", attendances);
        } catch (Exception e) {
            logger.error("获取学生考勤记录失败", e);
            return ApiResponse.error(500, "获取学生考勤记录失败: " + e.getMessage());
        }
    }
} 